/*
 * Copyright (c) 2011 Team A CS2340. All rights reserved.
 */

package edu.gatech.teama.doctorsoffice.client.presenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;

import edu.gatech.teama.doctorsoffice.model.Appointment;
import edu.gatech.teama.doctorsoffice.model.Patient;

/**
 * The Class AppointmentRequestPresenter. For use with appointment requests
 * @author TEAMA
 * @version 1.0
 */
public class AppointmentRequestPresenter { // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.alwaysOverridetoString.alwaysOverrideToString

	/**
	 * Gets the available times.
	 *
	 * @param date the date
	 * @return the available times
	 */
	public List<LocalTime> getAvailableTimes(LocalDateTime date) {
		final ServerConnection sc = ServerConnection.getInstance();
		final List<LocalDateTime> busyTimes = 
			sc.getBusyTimesForDate(date.toString("MMddyyyy"));
		final Map<Integer, LocalTime> availableTimes = new HashMap<Integer, LocalTime>();
		for (int hour = sc.getOfficeOpenTime().getHourOfDay();
			hour < sc.getOfficeCloseTime().getHourOfDay(); hour++) {
			availableTimes.put(hour, new LocalTime(hour, 0, 0));
		}
		
		for (LocalDateTime time : busyTimes) {
			availableTimes.remove(time.getHourOfDay());
		}
		
		return new ArrayList<LocalTime>(availableTimes.values());
		
	}
	
	/**
	 * Adds the appointment.
	 *
	 * @param date the date
	 * @param time the time
	 * @param reason the reason
	 */
	public void addAppointment(LocalDate date, LocalTime time, String reason) {
		final ServerConnection sc = ServerConnection.getInstance();
		final Appointment appointment = 
			new Appointment(sc.getLoggedInUser().getUsername(),
				new LocalDateTime(date.getYear(), date.getMonthOfYear(),
				date.getDayOfMonth(), time.getHourOfDay(), time.getMinuteOfHour()),
				reason);
		
		sc.addAppointment((Patient) sc.getLoggedInUser(), appointment);
	}
}
